import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:star_author/config/ui_constant.dart';
import 'package:star_author/utils/common_util/log_util.dart';
import 'package:star_author/utils/common_util/toast/custom_toast.dart';
import 'package:star_author/utils/http/http_util.dart';
import 'package:star_author/widgets/empty_view/h_empty_view.dart';
import 'package:star_author/widgets/empty_view/v_empty_view.dart';

class BidderReportBottomSheetHandler {
  static void showSheet(Map data, Function callback) {
    Get.bottomSheet(BidderReportBottomSheet(data), isScrollControlled: true)
        .then((value) {
      if (value == true) {
        callback();
      }
    });
  }
}

///拒绝回流原因
class BidderReportBottomSheet extends StatefulWidget {
  final Map data;

  const BidderReportBottomSheet(this.data, {Key? key}) : super(key: key);

  @override
  State<BidderReportBottomSheet> createState() =>
      _BidderReportBottomSheetState();
}

class _BidderReportBottomSheetState extends State<BidderReportBottomSheet> {
  List titleList = [
    {'abnormal_type': 'seller_bid_up', 'description': '卖家托价'},
    // {'abnormal_type': 'buyer_siege', 'description': '买家围标'},
    {'abnormal_type': 'other', 'description': '其他'},
  ];

  int? clickIndex;

  TextEditingController controller = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        FocusScopeNode currentFocus = FocusScope.of(context);
        if (!currentFocus.hasPrimaryFocus &&
            currentFocus.focusedChild != null) {
          FocusManager.instance.primaryFocus!.unfocus();
        }
      },
      behavior: HitTestBehavior.opaque,
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 12.w),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.vertical(
            top: Radius.circular(16.w),
          ),
        ),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            VEmptyView(16.w),
            Text(
              "异常举报",
              style: TextStyle(
                fontSize: 16.sp,
                fontWeight: FontWeight.w500,
                color: UIConstant.color33,
              ),
            ),
            VEmptyView(10.w),
            Text(
              '发现竞拍异常可向平台举报，平台核实后将进行相应处理',
              style: TextStyle(
                fontSize: 12.sp,
                color: UIConstant.color99,
              ),
            ),
            VEmptyView(24.w),
            ListView.builder(
              itemBuilder: (ctx, index) {
                return buildItem(index);
              },
              itemCount: titleList.length,
              shrinkWrap: true,
              padding: EdgeInsets.zero,
            ),
            if (clickIndex != null &&
                titleList[clickIndex!]['abnormal_type'] == "other")
              Container(
                width: 346.w,
                height: 78.w,
                decoration: BoxDecoration(
                  color: UIConstant.colorF5F5,
                ),
                padding: EdgeInsets.all(10.w),
                child: TextField(
                  decoration: InputDecoration(
                    hintText: "简要描述要举报的异常情况",
                    border: InputBorder.none,
                    contentPadding: EdgeInsets.zero,
                    hintStyle: TextStyle(
                      color: UIConstant.color99,
                      fontSize: 14.sp,
                    ),
                    labelStyle: TextStyle(
                      fontSize: 14.sp,
                    ),
                  ),
                  controller: controller,
                  maxLength: 200,
                  maxLines: 3,
                ),
              ),
            VEmptyView(18.w),
            GestureDetector(
              behavior: HitTestBehavior.opaque,
              onTap: () {
                Map? clickItem;
                if (clickIndex == null) {
                  CustomToast.show("请选择举报原因");
                  return;
                }

                clickItem = titleList[clickIndex!];
                if (clickItem == null) {
                  CustomToast.show("请选择举报原因");
                  return;
                }

                Map requestMap = clickItem;

                //处理inputText数据
                if (clickItem['abnormal_type'] == 'other') {
                  String inputString = controller.text.trim();
                  if (inputString.isNotEmpty) {
                    requestMap['description'] = inputString;
                  } else {
                    CustomToast.show("请填写其他原因");
                    return;
                  }
                }

                HttpUtil.commonRequest(
                  context,
                  '/api/v1/backflow_abnormal_reports',
                  method: HttpUtil.MethodPost,
                  data: {
                    'backflow_id': widget.data['id'],
                    ...requestMap,
                  },
                ).then((value) {
                  EasyLoading.showToast('举报成功');
                  Get.back(result: true);
                });
              },
              child: Container(
                width: 351.w,
                height: 44.w,
                alignment: Alignment.center,
                decoration: BoxDecoration(
                  color: UIConstant.colorEBA07,
                  borderRadius: BorderRadius.all(
                    Radius.circular(1000),
                  ),
                ),
                child: Text(
                  "立即举报",
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 16.sp,
                  ),
                ),
              ),
            ),
            VEmptyView(20.w + ScreenUtil().bottomBarHeight),
          ],
        ),
      ),
    );
  }

  Widget buildItem(int index) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () {
        LogUtil.v('object$index');
        clickIndex = index;
        setState(() {});
      },
      child: Container(
        margin: EdgeInsets.only(
            bottom: (index != titleList.length - 1) ? 20.w : 10.w),
        child: Row(
          children: [
            clickIndex == index
                ? Image.asset(
                    "images/ic_expected_recieved_reason_lt.png",
                    width: 20.w,
                  )
                : Container(
                    width: 20.w,
                    height: 20.w,
                    decoration: BoxDecoration(
                      border: Border.all(color: UIConstant.color97, width: 1.w),
                      borderRadius: BorderRadius.all(
                        Radius.circular(100),
                      ),
                    ),
                  ),
            HEmptyView(10.w),
            Text(
              titleList[index]['description'],
              style: TextStyle(
                fontSize: 16.sp,
                color: UIConstant.color33,
              ),
            ),
          ],
        ),
      ),
    );
  }
}
